package com.lwj.liwanjia.view.activity.personal;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.ShouCangBaoBeiAdapter;
import com.lwj.liwanjia.adapter.ShouCangDianPuAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.CenterBean02;
import com.lwj.liwanjia.bean.DpCollectBean;
import com.lwj.liwanjia.bean.PersonalHomeBean;
import com.lwj.liwanjia.config.OnItemClickListener;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.dianpu.DianpuActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/9.
 */

public class ShouCangActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(ShouCangActivity.this, "宝贝收藏数据获取失败", Toast.LENGTH_SHORT).show();
                    break;

                case 1:

                    int size = baoBeiList.size();
                    if (baoBeiList != null && baoBeiList.size() > 0) {
                        tvNoShouchang.setVisibility(View.GONE);
                        mRecyclerView.setVisibility(View.VISIBLE);
                        LinearLayoutManager manager = new LinearLayoutManager(ShouCangActivity.this);
                        mRecyclerView.setLayoutManager(manager);
                        baoBeiAdapter = new ShouCangBaoBeiAdapter(ShouCangActivity.this, baoBeiList);
                        mRecyclerView.setAdapter(baoBeiAdapter);
                        baoBeiAdapter.setListener(new OnItemClickListener() {
                            @Override
                            public void OnItemClick(View view, int position) {
                                Intent intent = new Intent(ShouCangActivity.this, ShangpinDetailActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("shopGoodsCode", baoBeiList.get(position).getCommodityCode());
                                bundle.putString("commodityType", baoBeiList.get(position).getCommodityType());
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    }else{
                        tvNoShouchang.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    if (dianPuList != null && dianPuList.size() > 0) {
                        tvNoShouchang.setVisibility(View.GONE);
                        mRecyclerView.setVisibility(View.VISIBLE);
                        LinearLayoutManager manager = new LinearLayoutManager(ShouCangActivity.this);
                        mRecyclerView.setLayoutManager(manager);
                        dianPuAdapter = new ShouCangDianPuAdapter(ShouCangActivity.this, dianPuList);
                        mRecyclerView.setAdapter(dianPuAdapter);
                        dianPuAdapter.setListener(new OnItemClickListener() {
                            @Override
                            public void OnItemClick(View view, int position) {
                                startActivity(new Intent(ShouCangActivity.this, DianpuActivity.class).putExtra("shopInfoId", dianPuList.get(position).getShopInfoId()));
                            }
                        });
                    }else{
                        tvNoShouchang.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                    }
                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private LinearLayout ll_left;//宝贝收藏的容器
    private LinearLayout ll_right;//店铺收藏的容器
    private TextView tv_baoBei;//宝贝字段
    private TextView tv_dianPu;//店铺字段
    private View view_baoBei;//宝贝下划线
    private View view_dianPu;//店铺下划线
    private TextView tvNoShouchang;
    private RecyclerView mRecyclerView;
    private ShouCangBaoBeiAdapter baoBeiAdapter;//宝贝的适配器
    private ShouCangDianPuAdapter dianPuAdapter;//店铺的适配器
    private List<CenterBean02.DataBean> baoBeiList;//宝贝的数据集合
    private List<DpCollectBean.DataBean> dianPuList = new ArrayList<>();//店铺的数据集合

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_right = (LinearLayout) findViewById(R.id.ll_right);
        tv_baoBei = (TextView) findViewById(R.id.tv_baoBei);
        tv_dianPu = (TextView) findViewById(R.id.tv_dianPu);
        tvNoShouchang= (TextView) findViewById(R.id.tv_no_shouchang);
        view_baoBei = findViewById(R.id.view_baoBei);
        view_dianPu = findViewById(R.id.view_dianPu);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView01);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("我的收藏");

        tv_back.setOnClickListener(this);
        ll_left.setOnClickListener(this);
        ll_right.setOnClickListener(this);

        //初始化UI
        tv_baoBei.setTextColor(Color.parseColor("#EC921A"));
        view_baoBei.setVisibility(View.VISIBLE);
        view_baoBei.setBackgroundColor(Color.parseColor("#EC921A"));
        tv_dianPu.setTextColor(Color.GRAY);
        view_dianPu.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {

        //连网 获取宝贝收藏 的数据
        initBaoBei();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wo_de_shou_cang;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
            //宝贝
            case R.id.ll_left:

                //更改UI
                changeUi(tv_baoBei, view_baoBei, tv_dianPu, view_dianPu);

                //展示宝贝数据
                initBaoBei();

                break;
            //店铺
            case R.id.ll_right:

                //更改UI
                changeUi(tv_dianPu, view_dianPu, tv_baoBei, view_baoBei);

                //展示店铺数据
                initDianPu();

                break;
        }
    }

    //更改UI
    private void changeUi(TextView tv_01, View view_01, TextView tv_02, View view_02) {

        tv_01.setTextColor(Color.parseColor("#EC921A"));
        view_01.setVisibility(View.VISIBLE);
        view_01.setBackgroundColor(Color.parseColor("#EC921A"));
        tv_02.setTextColor(Color.GRAY);
        view_02.setVisibility(View.GONE);
    }

    //连网 获取宝贝收藏 的数据
    private void initBaoBei() {

        String url = URLConstant.BASE_URL + "collect/userGoodsCollect.do";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                .add("type", "宝贝收藏")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "个人中心数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "个人中心数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                    parseBaoBei(result);
                } catch (Exception e) {

                }


            }
        });

    }

    //解析宝贝
    private void parseBaoBei(String result) {

        Gson gson = new Gson();
        CenterBean02 bean = gson.fromJson(result, CenterBean02.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    baoBeiList = new ArrayList<>();

                    for (int i = 0; i < bean.getData().size(); i++) {

                        CenterBean02.DataBean dataBean = bean.getData().get(i);
                        baoBeiList.add(dataBean);
                    }

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }

    //连网 获取店铺收藏 的数据
    private void initDianPu() {
        String url = URLConstant.BASE_URL + "collect/userShopCollectInfo.do";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient = new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token", token)
                // .add("type", "店铺收藏")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall = mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "个人中心数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "个人中心数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                    parseDiapu(result);
                } catch (Exception e) {
                    //Log.d("Tag", e.toString());
                }


            }
        });

    }

    private void parseDiapu(String result) {
        Gson gson = new Gson();
        DpCollectBean dpCollectBean = gson.fromJson(result, DpCollectBean.class);

        if (dpCollectBean != null) {

            switch (dpCollectBean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    dianPuList = new ArrayList<>();

                    for (int i = 0; i < dpCollectBean.getData().size(); i++) {
                        DpCollectBean.DataBean dataBean = dpCollectBean.getData().get(i);
                        dianPuList.add(dataBean);
                    }
                    mHandler.sendEmptyMessage(2);

                    break;
            }
        }
    }
}
